package com.src.myapplication.ui.moudle;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.Toast;

import com.src.myapplication.R;
import com.src.myapplication.base.IUserLoginBiz;
import com.src.myapplication.base.OnLoadLiveListListener;
import com.src.myapplication.bean.CaiYiben;
import com.src.myapplication.bean.FuJinben;
import com.src.myapplication.bean.FuJingDeRenben;
import com.src.myapplication.bean.GuanZhuben;
import com.src.myapplication.bean.MingXingben;
import com.src.myapplication.bean.PaiHangBangben;
import com.src.myapplication.bean.ReMenben;
import com.src.myapplication.bean.TuiJianben;
import com.src.myapplication.bean.XinRenben;
import com.src.myapplication.db.PersonDao;
import com.src.myapplication.http.OkHttpClientUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/10/18.
 */
public class Mode implements IUserLoginBiz {
    private SQLiteDatabase mDb;
    private PersonDao mDao;
    private SimpleCursorAdapter mAdapter;
    private Cursor mPerson;


    @Override
    public void log(final String url, final Class<? extends Object> clasz, final OnLoadLiveListListener listener) {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    String stringFromUrl = OkHttpClientUtils.getStringFromUrl(url);

                    subscriber.onNext(stringFromUrl);
                    subscriber.onCompleted();
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }
            }
        }).subscribeOn(Schedulers.newThread()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Action1<String>() {
                              @Override
                              public void call(String s) {
                                  JSONObject object = null;
                                  if (s != null) {
                                      if (clasz == TuiJianben.class) {

                                          listener.onSuccess(s);

                                      } else if (clasz == ReMenben.class) {
                                          try {
                                              object = new JSONObject(s);
                                              String string2 = object.getString("info");
                                              JSONObject object2 = new JSONObject(string2);
                                              String string1 = object2.getString("livelist");
                                              listener.onSuccess(string1);
                                          } catch (JSONException e) {
                                              e.printStackTrace();
                                              listener.onFailure("数据解析异常", e);
                                          }

                                      } else if (clasz == XinRenben.class) {
                                          try {
                                              object = new JSONObject(s);
                                              String string2 = object.getString("info");
                                              JSONObject object2 = new JSONObject(string2);
                                              String string1 = object2.getString("livelist");
                                              listener.onSuccess(string1);
                                          } catch (JSONException e) {
                                              e.printStackTrace();
                                              listener.onFailure("数据解析异常", e);
                                          }

                                      } else if (clasz == FuJinben.class) {
                                          try {
                                              object = new JSONObject(s);
                                              String string2 = object.getString("info");
                                              JSONObject object2 = new JSONObject(string2);
                                              String string1 = object2.getString("livelist");
                                              listener.onSuccess(string1);
                                          } catch (JSONException e) {
                                              e.printStackTrace();
                                              listener.onFailure("数据解析异常", e);
                                          }

                                      } else if (clasz == GuanZhuben.class) {
                                          listener.onSuccess(s);
                                      } else if (clasz == MingXingben.class) {
                                          listener.onSuccess(s);
                                      } else if (clasz == CaiYiben.class) {
                                          listener.onSuccess(s);
                                      } else if (clasz == FuJingDeRenben.class) {
                                          listener.onSuccess(s);
                                      } else if (clasz == PaiHangBangben.class) {
                                          listener.onSuccess(s);
                                      }
                                  }
                              }
                          }, new Action1<Throwable>() {
                              @Override
                              public void call(Throwable throwable) {
                                  listener.onFailure("网络异常", throwable);
                              }
                          }

                );
    }

    public boolean save(Context context, String[] data) {
        String id = data[0];
        String name = data[1];
        String imag = data[2];
        String url = data[3];

        mDao = new PersonDao(context);
        mDb = mDao.getDb();
        ContentValues values = new ContentValues();
        //集合中的key就是表中的列名.
        values.put("uid", id);
        values.put("name", name);
        values.put("imag", imag);
        values.put("url", url);
        //返回值:最后那一行的行号.
        long idd = mDb.insert("person", null, values);

        if (idd > 0) {
            Toast.makeText(context, "关注成功", Toast.LENGTH_LONG).show();
            mPerson = mDb.query("person", new String[]{"uid", "name"}, null, null, null, null, null);
            mAdapter.changeCursor(mPerson);
            return true;

        } else {
            Toast.makeText(context, "关注失败", Toast.LENGTH_LONG).show();
            return false;
        }
    }


    public boolean delete(Context context, String uid) {
        int id = mDb.delete("person", "uid = ?", new String[]{uid});

        if (id > 0) {
            Toast.makeText(context, "取消关注", Toast.LENGTH_LONG).show();
            mPerson = mDb.query("person", new String[]{"uid", "name"}, null, null, null, null, null);
            mAdapter.changeCursor(mPerson);
            return true;

        } else {
            Toast.makeText(context, "取消失败", Toast.LENGTH_LONG).show();
            return false;
        }
    }


    public SimpleCursorAdapter query(Context context) {
        mDao = new PersonDao(context);
        mDb = mDao.getDb();
        mPerson = mDb.query("person", null, null, null, null, null, null);
        if (mPerson != null) {
            mAdapter = new SimpleCursorAdapter(context, R.layout.fragment_fujinderen_list, mPerson, new String[]{"uid", "name",}, new int[]{R.id.sdv, R.id.name}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            return mAdapter;
        }
        return null;
    }

    public boolean query(Context context, String uid) {
        Cursor cursor = mDb.query("person", new String[]{"uid"}, "uid = ?", new String[]{uid}, null, null, null);
        if (cursor.getCount() == 0) {
            return false;
        } else if (cursor.moveToNext()) {
            int columnIndex = cursor.getColumnIndex(uid);
            String string = cursor.getString(columnIndex);
            if (string.equals(uid)) {
                Toast.makeText(context, "已被关注", Toast.LENGTH_LONG).show();
                return true;
            } else {
                return false;
            }

        }
        return true;
    }
}



